#!/usr/bin/perl -w

#Yet another c analyzer							#
#This program is free software: you can redistribute it and/or modify	#
#it under the terms of the GNU General Public License as published by	#
#the Free Software Foundation, either version 3 of the License, or	#
#(at your option) any later version.					#
#									#
#This program is distributed in the hope that it will be useful,	#
#but WITHOUT ANY WARRANTY; without even the implied warranty of		#
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		#
#GNU General Public License for more details.				#
#									#
#Copyright (C) 2011 Nazarov Yuriy					#
#									#
#You should have received a copy of the GNU General Public License	#
#along with this program.  If not, see <http://www.gnu.org/licenses/>.	#

#Use 8 position tab and max 120 symbols per line

# check command-line args
if (($#ARGV == 0) && ($ARGV[0] =~ m/(-h)|(--help)/)){
	print "Make trash from code ^_^\n";
	exit;
}
if (($#ARGV == 1) && ("$ARGV[0] $ARGV[1]" =~ m/in=(.+) out=(.+)/)) {		#TODO: reimplement this!!!
	$in_file_name	= $1;
	$out_file_name	= $2;
} else {
	print "\nPlease usage: <prog_name>.pl in=<in_file.c> out=<out_file.c>\n";
	exit;
}

#maybe many files for you purposes
open(MYINPUTFILE, "< $in_file_name") or die "can't open file $in_file_name for read";
open(MYOUTPUTFILE, "> $out_file_name") or die "can't open file $out_file_name for write";

#TODO:
my @vars;
my @functions;

#USED:
my @comments;
my $code = "";
my @deffs;
my @includes_l;
my @includes_g;

#do some processing here:
sub code {
	if($_[0] =~ m/(.*)\/\/(.*)/){
		code($1);
		comm($2);
	} else {
		$cline = $_[0];
		while($cline =~ s/\s\s+/ /){}
		$code .= "$cline ";
	}
}

#do some processing here:
sub comm {
	push(@comments, $_[0]);
}

#do some processing here:
sub inc_global {
	push(@includes_g, $_[0]);
}

#do some processing here:
sub inc_local {
	push(@includes_l, $_[0]);
}

#do some processing here:
sub deff {
	push(@deffs, $_[0]);
}

use constant {
	STATE_CODE	=> 0,
	STATE_COMMENT	=> 1,
	STATE_IFDEF	=> 2,
};
my $state = STATE_CODE;

while(<MYINPUTFILE>)
{
	my $line = $_;
	chomp($line);
	
	if($state == STATE_CODE) {
#parse includes
		if($line =~ m/#include "(.+)"/){
			inc_local($1);
		} elsif($line =~ m/#include <(.+)>/){
			inc_global($1);
#parse defines
		} elsif($line =~ m/#define (.+)/){
			deff($1);
#comments inside line
		} elsif($line =~ m/(.*)\/\*(.*)\*\/(.*)/) {
			code($1);
			comm($2);
			code($3);
#multiline comments started
		} elsif($line =~ m/(.*)\/\*(.*)/) {
			code($1);
			comm($2);
			$state = 1;
		} else {
			code($line);
		}
	} elsif($state == STATE_COMMENT) {
#multiline comments ended
		if($line =~ m/(.*)\*\/(.*)/) {
			comm($1);
			code($2);
			$state = 0;
		} else {
			comm($line);
		}
	} elsif($state == STATE_IFDEF) {
		;
	} else {
		;
	}
}
for(@includes_g){
	print MYOUTPUTFILE "#include <$_>\n";
}
for(@includes_l){
	print MYOUTPUTFILE "#include \"$_\"\n";
}
for(@deffs){
	print MYOUTPUTFILE "#define $_\n";
}
print MYOUTPUTFILE $code;

close(MYINPUTFILE);
close(MYOUTPUTFILE);
